<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/4/4
 * Time: 15:45
 */

namespace app\home\controller;


class News extends Base
{
    private $obj;
    public function _initialize()
    {
        parent::_initialize();
        $this->assign('time',time());
        $this->obj = model('article');
    }
    public function index(){
        $slide = action('Poster/index',['id'=>3,'num'=>5],'event');//切换广告
        $this->assign('slides',$slide);
        return $this->fetch();
    }
    public function lists(){
        $keyword = input('get.keyword');
        $cate = cache('articleCate');
        $cate_id = input('param.cate_id/d',0);
        $where = 'status=1 and create_time <'.time();
        $info['name'] = '新闻资讯';
        if($cate_id){
            if(isset($cate['tree'][$cate_id]) && isset($cate['tree'][$cate_id]['_child'])){
                $child = $cate['tree'][$cate_id]['_child'];
                $child_id = array_keys($child);
                $child_id[] = $cate_id;
                $where .= " and cate_id in (".implode(',',$child_id).")";

            }else{
                $where .= " and cate_id = ".$cate_id;
            }
            $cate['cate'][$cate_id]['pid'] && $cate_id = $cate['cate'][$cate_id]['pid'];
            $info['name'] = $cate['cate'][$cate_id]['name'];
        }
        if($keyword){
            $where .= " and title like '%".$keyword."%'";
            $this->assign('keyword',$keyword);
        }
        $this->setSeo($info,'name');
        $this->assign('cate_id',$cate_id);
        $this->assign('where',$where);
        return $this->fetch();
    }
    public function detail(){
        $id = input('param.id/d',0);
        $info='';$house_info='';
        if($id){
            $info = $this->obj->where(['status'=>1,'id'=>$id])->find();
            if(!$info){
                return $this->fetch('public/404');
            }
            $this->obj->where(['status'=>1,'id'=>$id])->setInc('hits');
            if($info['house_id']){
                $house_info = model('house')->field('id,title,img,city_name,area_name,average_price,address,sale_phone')->where(['id'=>$info['house_id'],'status'=>1])->find();
                $fuhao = empty($info['seo_keys'])?'':',';
                $info['seo_keys'] .= $fuhao.$house_info['title'];
            }
        }else{
            return $this->fetch('public/404');
        }
        $love_lists = $this->obj->where(['cate_id'=>$info['cate_id'],'status'=>1])->field('id,title,create_time')->order('id desc')->limit(8)->select();
        $this->setSeo($info);
        $this->assign('info',$info);
        $this->assign('house',$house_info);
        $this->assign('love_lists',$love_lists);
        $this->assign('prenews',$this->getPreNext($info['id'],$info['cate_id']));
        $this->assign('nextnews',$this->getPreNext($info['id'],$info['cate_id'],'next'));
        return $this->fetch();
    }
    /**
     * @param $id
     * @param $cate_id
     * @param string $type
     * @return string
     * 获取 上一篇，下一篇文章
     */
    private function getPreNext($id,$cate_id,$type='pre'){
        if($type == 'pre'){
            $map['id'] = ['lt',$id];
            $sort      = 'id desc';
        }else{
            $map['id'] = ['gt',$id];
            $sort      = 'id asc';
        }
        $map['status']  = 1;
        $map['cate_id'] = $cate_id;
        $map['create_time'] = ['lt',time()];
        $info           = $this->obj->where($map)->field('id,title')->order($sort)->find();
        if($info){
            $str = "<a href='".url('News/detail',['id'=>$info['id']])."' title='".$info['title']."'>".$info['title']."</a>";
        }else{
            $str = '没有了';
        }
        return $str;
    }

}